import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/widgets/common_btn.dart';
import 'package:photo_manager/photo_manager.dart';

class CommonPhotoPermissionBtnWidget extends StatefulWidget {
  const CommonPhotoPermissionBtnWidget({super.key});

  @override
  State<CommonPhotoPermissionBtnWidget> createState() => _CommonPhotoPermissionBtnWidgetState();
}

class _CommonPhotoPermissionBtnWidgetState extends State<CommonPhotoPermissionBtnWidget> with WidgetsBindingObserver {
  bool isOpenSetting = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this); // 添加观察者监听
  }

  @override
  void dispose() {
    isOpenSetting = false;
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) async {
    if (state == AppLifecycleState.resumed && isOpenSetting) {//应用处于前台时，检查相册权限
      isOpenSetting = false;
      // eventListener.emit(Constant.CHECKPERMISSION);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('请允许使用你的相册权限', style: TextStyle(fontSize: 28.sp, color: Colors.grey)),
        SizedBox(height: 12.w),
        CommonBtnWidget(txt: '去开启', fontSize: 30.sp, size: Size(200.w, 64.w), onTap: () {
          isOpenSetting = true;
          PhotoManager.openSetting();
        })
      ],
    );
  }
}
